package cn.car91.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import cn.car91.R;
import cn.car91.bean.User;
import cn.car91.http.HttpHandler;
import cn.car91.utils.Car91JsonUtil;
import cn.car91.utils.RegexUtil;
import cn.car91.view.HerilyAlertDialog;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;

public class UserLoginActivity extends BaseActivity {
    public static final String action = "user.login.broadcast.action";

    @ViewInject(R.id.reg_btn)
    View regBtn;
    @ViewInject(R.id.forget_btn)
    View forgetBtn;
    @ViewInject(R.id.login_btn)
    View loginBtn;

    @ViewInject(R.id.user_name)
    EditText usernameView;
    @ViewInject(R.id.user_password)
    EditText passwordView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_login);
        super.onCreate(savedInstanceState);

        try {
            String uname = getCar91Application().getUser().getName();
            usernameView.setText(uname);
            usernameView.selectAll();
        } catch (Exception e) {
        }

        ((TextView) findViewById(R.id.header_text)).setText(R.string.login);

        RegForgetClickEvent regClickEvent = new RegForgetClickEvent();
        regClickEvent.setActivity(UserRegActivity.class);
        regBtn.setOnClickListener(regClickEvent.onClickListener);

        RegForgetClickEvent forgetClickEvent = new RegForgetClickEvent();
        forgetClickEvent.setActivity(UserForgetActivity.class);
        forgetBtn.setOnClickListener(forgetClickEvent.onClickListener);

        loginBtn.setOnClickListener(onLoginClickListener);
    }

    private OnClickListener onLoginClickListener = new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            String name = usernameView.getText().toString();
            String pwd = passwordView.getText().toString();
            Boolean canLogin = true;
            if (RegexUtil.isNull(name)) {
                canLogin = false;
                new HerilyAlertDialog.Builder(UserLoginActivity.this).setTitle("提示").setMessage("请输入登录账户名").create().show();
                usernameView.requestFocus();
            } else if (RegexUtil.isNull(pwd)) {
                canLogin = false;
                new HerilyAlertDialog.Builder(UserLoginActivity.this).setTitle("提示").setMessage("请输入密码").create().show();
                passwordView.requestFocus();
            }

            if (canLogin) {
                try {
                    HttpHandler.httpHandler.loginUser(name, pwd, callbackUser);
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        }

        private RequestCallBack callbackUser = new RequestCallBack() {
            @Override
            public void onStart() {
                // TODO Auto-generated method stub
                super.onStart();
                loginBtn.setEnabled(false);
            }

            @Override
            public void onFailure(HttpException arg0, String arg1) {
                // TODO Auto-generated method stub
                loginBtn.setEnabled(true);
                //Toast.makeText(UserLoginActivity.this, this.getRequestUrl(), Toast.LENGTH_LONG).show();
                Toast.makeText(UserLoginActivity.this, "网络异常，请稍后再试", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onSuccess(ResponseInfo arg0) {
                // TODO Auto-generated method stub
                try {
                    User user = (User) Car91JsonUtil.getDTO(arg0.result.toString(), User.class);
                    if (user == null) {
                        new HerilyAlertDialog.Builder(UserLoginActivity.this).setTitle("提示").setMessage("用户名或密码错误\n亦或被限制登录").create().show();
                    } else {
                        getCar91Application().setUser(user);
                        Intent intent = new Intent();
                        intent.setAction(action);
                        intent.putExtra("loginStatus", true);
                        sendBroadcast(intent);
                        setResult(Car91ResultCode.Refresh_User_Code);
                        finish();
                    }
                } catch (Exception e) {
                    new HerilyAlertDialog.Builder(UserLoginActivity.this).setTitle("提示").setMessage("用户名或密码错误").create().show();
                }
                loginBtn.setEnabled(true);
            }
        };
    };

    private class RegForgetClickEvent {
        private Class activityClass;

        public RegForgetClickEvent() {
        }

        public RegForgetClickEvent(Class activityClass) {
            this.activityClass = activityClass;
        }

        public Class getActivity() {
            return activityClass;
        }

        public void setActivity(Class activityClass) {
            this.activityClass = activityClass;
        }

        public OnClickListener onClickListener = new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (activityClass != null) {
                    Intent intent = new Intent();
                    intent.setClass(getApplicationContext(), activityClass);
                    startActivity(intent);
                }
            }
        };
    }
}
